<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="广告名称" prop="adName">
        <el-input
          v-model="queryParams.adName"
          placeholder="请输入广告名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option :value="1" label="启用"></el-option>
          <el-option :value="0" label="禁用"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择开始时间"
        />
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择结束时间"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['marketing:splashAd:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="splashAdList">
      <el-table-column label="ID" align="center" prop="id" width="60" />
      <el-table-column label="广告名称" align="center" prop="adName" />
      <el-table-column label="广告图片" align="center" prop="adImage">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.adImage"
            fit="cover"
            class="img"
            :preview-src-list="[scope.row.adImage]"
          />
        </template>
      </el-table-column>
      <el-table-column label="跳转类型" align="center" prop="jumpType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.jumpType === 0">不跳转</el-tag>
          <el-tag v-else-if="scope.row.jumpType === 1">小程序页面</el-tag>
          <el-tag v-else-if="scope.row.jumpType === 2">网页链接</el-tag>
          <el-tag v-else-if="scope.row.jumpType === 3">商品详情</el-tag>
          <el-tag v-else-if="scope.row.jumpType === 4">活动页面</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="跳转路径"
        align="center"
        prop="jumpPath"
        show-overflow-tooltip
      />

      <el-table-column
        label="开始时间"
        align="center"
        prop="startTime"
        width="160"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.startTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="160"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.endTime) }}
        </template>
      </el-table-column>

      <el-table-column
        label="显示时长(秒)"
        align="center"
        prop="displayDuration"
      />
      <el-table-column label="排序值" align="center" prop="sortOrder" />

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['marketing:splashAd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['marketing:splashAd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="广告名称" prop="adName">
          <el-input v-model="form.adName" placeholder="请输入广告名称" />
        </el-form-item>

        <el-form-item label="广告图片" prop="adImage">
          <ImageUpload v-model="form.adImage" />
        </el-form-item>

        <el-form-item label="跳转类型" prop="jumpType">
          <el-select
            v-model.number="form.jumpType"
            placeholder="请选择跳转类型"
          >
            <el-option :label="'不跳转'" :value="0" />
            <el-option :label="'小程序页面'" :value="1" />
            <el-option :label="'网页链接'" :value="2" />
            <el-option :label="'商品详情'" :value="3" />
            <el-option :label="'活动页面'" :value="4" />
          </el-select>
        </el-form-item>

        <el-form-item
          v-if="form.jumpType !== 0"
          label="跳转路径"
          prop="jumpPath"
        >
          <el-input v-model="form.jumpPath" placeholder="请输入跳转路径" />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                v-model="form.startTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker
                v-model="form.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="显示时长(秒)" prop="displayDuration">
          <el-input-number v-model="form.displayDuration" :min="1" :max="30" />
        </el-form-item>

        <el-form-item label="排序值" prop="sortOrder">
          <el-input-number v-model="form.sortOrder" :min="0" />
        </el-form-item>

        <el-form-item label="平台" prop="platform">
          <el-select v-model="form.platform" placeholder="请选择平台">
            <el-option label="全部" value="all" />
            <el-option label="H5" value="h5" />
            <el-option label="小程序" value="xcx" />
          </el-select>
        </el-form-item>

        <el-form-item label="是否允许跳过" prop="skipEnabled">
          <el-radio-group v-model.number="form.skipEnabled">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model.number="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSplashAd,
  getSplashAd,
  addSplashAd,
  updateSplashAd,
  delSplashAd,
  enableSplashAd,
  disableSplashAd,
} from "@/api/system/splashAd";
import { allPositionList } from "@/api/mallGuan/bannerPosition";
import RelatedGoods from "../../components/relatedGoods.vue";
import { uploadimage } from "@/api/system/lunbo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { allChannelList } from "@/api/mallGuan/channel";
import ImageUpload from "@/components/ImageUpload";
export default {
  name: "Banner",
  components: {
    RelatedGoods,
    Treeselect,
    ImageUpload,
  },
  data() {
    return {
      loading: true,
      showSearch: true,
      splashAdList: [],
      total: 0,
      open: false,
      title: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adName: null,
        status: null,
        startTime: null,
        endTime: null,
      },
      form: {
        adName: null,
        adImage: null,
        jumpType: 0,
        jumpPath: null,
        startTime: null,
        endTime: null,
        displayDuration: 3,
        sortOrder: 0,
        status: 1,
        platform: "all",
        skipEnabled: 1,
      },
      rules: {
        adName: [
          { required: true, message: "请输入广告名称", trigger: "blur" },
        ],
        adImage: [
          { required: true, message: "请上传广告图片", trigger: "blur" },
        ],
        jumpType: [
          { required: true, message: "请选择跳转类型", trigger: "change" },
        ],
        startTime: [
          { required: true, message: "请选择开始时间", trigger: "change" },
        ],
        endTime: [
          { required: true, message: "请选择结束时间", trigger: "change" },
        ],
        displayDuration: [
          {
            required: true,
            message: "请输入显示时长",
            trigger: "blur",
            type: "number",
          },
        ],
        sortOrder: [
          {
            required: true,
            message: "请输入排序值",
            trigger: "blur",
            type: "number",
          },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listSplashAd(this.queryParams).then((response) => {
        this.splashAdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增开屏广告";
    },
    handleUpdate(row) {
      getSplashAd(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开屏广告";
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const api = this.form.id ? updateSplashAd : addSplashAd;
          api(this.form).then(() => {
            this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除广告 "${row.adName}"？`)
        .then(() => delSplashAd(row.id))
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
    },
    handleStatusChange(row) {
      const text = row.status === 1 ? "启用" : "禁用";
      const api = row.status === 1 ? enableSplashAd : disableSplashAd;
      this.$modal
        .confirm(`确认要${text} "${row.adName}" 吗？`)
        .then(() => api(row.id))
        .then(() => {
          this.$modal.msgSuccess(`${text}成功`);
        })
        .catch(() => {
          // 撤销状态切换
          row.status = row.status === 1 ? 0 : 1;
        });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        adName: null,
        adImage: null,
        jumpType: 0,
        jumpPath: null,
        startTime: null,
        endTime: null,
        displayDuration: 3,
        sortOrder: 0,
        status: 1,
        platform: "all",
        skipEnabled: 1,
      };
      this.resetForm("form");
    },
  },
};
</script>
<style lang="less">
.el-radio {
  margin-right: 20px;
}
.img {
  width: 80px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #ccc;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  border: 1px dashed #ccc;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
.image {
  width: 60px;
}
.image {
}
</style>
